package com.wzh.project.utils.ssh2;

import java.text.DecimalFormat;
import java.util.Timer;
import java.util.TimerTask;

import javax.servlet.http.HttpSession;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.jcraft.jsch.SftpProgressMonitor;
/**
 * 上传文件的进度监听
 * @author wzh
 *
 */
public class MySftpProgressMonitor extends TimerTask implements SftpProgressMonitor {
	
	private static final Logger logger = LogManager.getLogger(MySftpProgressMonitor.class);
	
	private HttpSession session;
	
	private long progressInterval = 1000; // 默认间隔时间为5秒

	private boolean isEnd = false; // 记录传输是否结束

	private long transfered; // 记录已传输的数据总大小

	private long fileSize; // 记录文件总大小

	private Timer timer; // 定时器对象

	private boolean isScheduled = false; // 记录是否已启动timer记时器
	
	private String pro;//当前上传进度
	
	public MySftpProgressMonitor(long size,HttpSession session){
		this.fileSize=size;
		this.session=session;
	}
	@Override
	public void run() {
		logger.debug("开始文件上传定时器");
		if (!isEnd()) { // 判断传输是否已结束
			long transfered = getTransfered();
			if (transfered != fileSize) { 
				// 判断当前已传输数据大小是否等于文件总大小
				sendProgressMessage(transfered);
			} else {
				setEnd(true); // 如果当前已传输数据大小等于文件总大小，说明已完成，设置end
			}
		} else {
			stop(); // 如果传输结束，停止timer记时器
			return;
		}
	}

	public void stop() {
		logger.debug("Try to stop progress monitor.");
		if (timer != null) {
			timer.cancel();
			timer.purge();
			timer = null;
			isScheduled = false;
		}
		logger.debug("Progress monitor stoped.");
	}

	public void start() {
		logger.debug("Try to start progress monitor.");
		if (timer == null) {
			timer = new Timer();
		}
		timer.schedule(this, 1000, progressInterval);
		isScheduled = true;
	}

	/**
	 * 打印progress信息
	 * 
	 * @param transfered
	 */
	private void sendProgressMessage(long transfered) {
		double d = ((double) transfered * 100) / (double) fileSize;
		DecimalFormat df = new DecimalFormat("#.##");
		pro = df.format(d);
		session.setAttribute("pro", pro);
		logger.debug("Sending progress message: " + pro + "%");
	}

	/**
	 * 实现了SftpProgressMonitor接口的count方法
	 */
	@Override
	public boolean count(long count) {
		if (isEnd())
			return false;
		if (!isScheduled) {
			start();
		}
		add(count);
		return true;
	}

	/**
	 * 实现了SftpProgressMonitor接口的end方法
	 */
	@Override
	public void end() {
		setEnd(true);
		logger.debug("上传到dumps文件成功");
	}

	private synchronized void add(long count) {
		transfered = transfered + count;
	}

	private synchronized long getTransfered() {
		return transfered;
	}

	public synchronized void setTransfered(long transfered) {
		this.transfered = transfered;
	}

	private synchronized void setEnd(boolean isEnd) {
		this.isEnd = isEnd;
		session.setAttribute("isEnd", "true");
	}

	private synchronized boolean isEnd() {
		return isEnd;
	}
	/**
	 * 给外部提供当前的进度
	 * @return
	 */
	public synchronized String getPro(){
		return pro;
	}

	@Override
	public void init(int op, String src, String dest, long max) {
		// 传输流的话没有用 只有传输文件可以使用
	}
}
